package com.outdoor.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.outdoor.common.constant.MessageConstant;
import com.outdoor.common.exception.PermissionDeniedException;
import com.outdoor.common.result.PageResult;
import com.outdoor.pojo.dto.TracePageQueryDTO;
import com.outdoor.pojo.dto.TracePublishDTO;
import com.outdoor.pojo.entity.Node;
import com.outdoor.pojo.entity.Trace;
import com.outdoor.pojo.entity.Track;
import com.outdoor.pojo.entity.User;
import com.outdoor.pojo.vo.TraceVO;
import com.outdoor.server.mapper.NodeMapper;
import com.outdoor.server.mapper.TraceMapper;
import com.outdoor.server.mapper.TrackMapper;
import com.outdoor.server.mapper.UserMapper;
import com.outdoor.server.service.TraceService;
import com.outdoor.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
@Slf4j
public class TraceServiceImpl implements TraceService {

    @Autowired
    private TraceMapper traceMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TrackMapper trackMapper;
    @Autowired
    private NodeMapper nodeMapper;



    /**
     * 发布路线
     * @param tracePublishDTO
     */
    @Transactional
    public void publishTrace( TracePublishDTO tracePublishDTO) {
        // 檢查用戶是否有发布路线的权限
        User user = userMapper.getUserById(tracePublishDTO.getUid());
        if (user == null || user.getPermissions() != 1) {
            throw new PermissionDeniedException(MessageConstant.PERMISSION_DENIED);
        }
        Track track = trackMapper.selectById(tracePublishDTO.getTrackId());
        Trace trace = new Trace();
        BeanUtils.copyProperties(track, trace);
        BeanUtils.copyProperties(tracePublishDTO, trace);
        trace.setPublished_time(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //默认路线状态为0启用
        trace.setStatus(0);
        trace.setLike(0);
        trace.setCollection(0);
        trace.setComment(0);
        trace.setUse(0);
        traceMapper.insert(trace);
    }

    @Override

    /**
     * 分页查询
     * @param tracePageQueryDTO
     * @return
     */
    public PageResult page(TracePageQueryDTO tracePageQueryDTO) {
        PageHelper.startPage(tracePageQueryDTO.getPage(), tracePageQueryDTO.getPageSize());
        Page<Trace> page = traceMapper.page(tracePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询路线
     * @param id
     * @return
     */
    public TraceVO getById(Integer id) {
        Trace trace = traceMapper.selectById(id);
        TraceVO traceVO = new TraceVO();
        BeanUtils.copyProperties(trace, traceVO);
        // 解析节点信息
        String nodeSequence = trace.getNode();
        if (nodeSequence != null && !nodeSequence.isEmpty()) {
            String[] nodeIds = nodeSequence.split(",");
            for (String nodeId : nodeIds) {
                Node node = nodeMapper.selectById(Long.valueOf(nodeId));
                if (node != null) {
                    traceVO.getNodes().add(node);
                }
            }
        }
        return traceVO;
    }

    /**
     * 路线启用禁用
     */
    public void updateStatus(Integer status, Long id) {
        Trace trace = new Trace();
        trace.setStatus(status);
        trace.setId(id);
        traceMapper.update(trace);
    }
} 